let prefix = "/domain/realtimealarm"

let dateTime = new Date().getTime();

let currentDate = format(dateTime);
// alert(currentDate)
// var endTime = format(dateTime);
// var preDate = new Date(dateTime - 24 * 60 * 60 * 1000);
// var beginTime = format(preDate);
let endTime = '';
let beginTime = '';
let alarmstatus = '';

$(function () {
    getQueren();
    getActive();
    getSum();
    load();
    getNum();

    layui.use(['form', 'laydate'], function(){
        let form = layui.form;
        let laydate = layui.laydate;
        //结束时间
        let endDate= laydate.render({
            elem: '#end',//时间框id值
            type: 'datetime',
            max: currentDate,//设置min默认最大值
            btns: ['clear', 'confirm'],
            done: function(value,date){
                startDate.config.max={
                    year:date.year,
                    month:date.month-1,//关键
                    date: date.date,
                    hours: 0,
                    minutes: 0,
                    seconds : 0
                }
            }
        });
        //日期范围-开始时间
        let startDate=laydate.render({
            elem: '#begin',//时间框id值
            type: 'datetime',
            max: currentDate,//设置一个默认最大值
            btns: ['clear', 'confirm'],
            done: function(value, date){
                endDate.config.min ={
                    year:date.year,
                    month:date.month-1, //关键
                    date: date.date,
                    hours: 0,
                    minutes: 0,
                    seconds : 0
                };
            }
        });
    });


    // let laydate = layui.laydate;
    //
    // //日期时间选择器
    // laydate.render({
    //     elem: '#begin'
    //     , type: 'datetime'
    //     ,max: currentDate
    //     ,btns: ['clear', 'confirm']
    //     ,done: function (value, date, endDate) {
    //         let startDate1 = new Date(value).getTime();
    //         let endTime1 = new Date($('#end').val()).getTime();
    //         if (endTime1 < startDate1) {
    //             layer.msg('结束时间不能小于开始时间');
    //             $("#begin").val('')
    //         }
    //     }
    // });
    //
    // //日期时间选择器
    // laydate.render({
    //     elem: '#end'
    //     , type: 'datetime'
    //     ,max: currentDate
    //     ,btns: ['clear', 'confirm']
    //     ,done: function (value, date, endDate) {
    //         let startDate1 = new Date($('#begin').val()).getTime();
    //         let endTime1 = new Date(value).getTime();
    //         if (endTime1 < startDate1) {
    //             layer.msg('结束时间不能小于开始时间');
    //             $("#end").val('')
    //         }
    //     }
    //     // , value: endTime
    //     // , isInitValue: false //是否允许填充初始值，默认为 true
    // });

    $('.filter-btn button').click(function () {
        $(this).toggleClass('btn-success').toggleClass('btn-white').siblings().removeClass('btn-success').addClass('btn-white');
    });
});


function load() {
    $('#exampleTable')
        .bootstrapTable(
            {
                method: 'get', // 服务器数据的请求方式 get or post
                url: prefix + "/list", // 服务器数据的加载地址
                //	showRefresh : true,
                //	showToggle : true,
                //	showColumns : true,
                iconSize: 'outline',
                toolbar: '#exampleToolbar',
                striped: true, // 设置为true会有隔行变色效果
                dataType: "json", // 服务器返回的数据类型
                pagination: true, // 设置为true会在底部显示分页条
                // queryParamsType : "limit",
                // //设置为limit则会发送符合RESTFull格式的参数
                singleSelect: false, // 设置为true将禁止多选
                // contentType : "application/x-www-form-urlencoded",
                // //发送到服务器的数据编码类型
                pageSize: 10, // 如果设置了分页，每页数据条数
                pageNumber: 1, // 如果设置了分布，首页页码
                //search : true, // 是否显示搜索框
                showColumns: false, // 是否显示内容下拉框（选择显示的列）
                sidePagination: "server", // 设置在哪里进行分页，可选值为"client" 或者 "server"
                queryParams: function (params) {
                    return {
                        //说明：传入后台的参数包括offset开始索引，limit步长，sort排序列，order：desc或者,以及所有列的键值对
                        limit: params.limit,
                        offset: params.offset,
                        // name:$('#searchName').val(),
                        // username:$('#searchName').val()
                        beginTime: beginTime,
                        endTime: endTime,
                        alarmstatus: alarmstatus,
                    };
                },
                onLoadSuccess: function (data) {
                },
                // //请求服务器数据时，你可以通过重写参数的方式添加一些额外的参数，例如 toolbar 中的参数 如果
                // queryParamsType = 'limit' ,返回参数必须包含
                // limit, offset, search, sort, order 否则, 需要包含:
                // pageSize, pageNumber, searchText, sortName,
                // sortOrder.
                // 返回false将会终止请求
                columns: [
                    {
                        field: 'alarmseq',
                        title: '告警ID'
                    },
                    {
                        field: 'province',
                        title: '省份'
                    },
                    {
                        field: 'netype',
                        title: '网元类型'
                    },
                    {
                        field: 'nename',
                        title: '网元名称'
                    },
                    {
                        field: 'rnetype',
                        title: '告警对象'
                    },
                    {
                        field: 'origseverity',
                        title: '告警级别',
                        formatter: warnLevel,
                    },
                    {
                        field: 'alarmtitle',
                        title: '告警标题'
                    },
                    {
                        field: 'eventtime',
                        title: '告警发生时间',
                        formatter: function (value, row, index) {
                            let str = value.substring(0,4)+'/'+ value.substring(4,6)+'/'+ value.substring(6,8)+" "+ value.substring(8,10)+':'+ value.substring(10,12)+':'+ value.substring(12,14)
                            let oldTime = (new Date(str)).getTime();
                            return format(oldTime);
                        }
                    },
                    {
                        field: 'locationinfo',
                        title: '具体问题'
                    },
                ]
            });
}

function reLoad() {
    $('#exampleTable').bootstrapTable('refresh');
}


function warnLevel(value, row, index) {
    if (value == 1) {
        return '<span class="label label-danger">一级告警</span>'
    } else if (value == 2) {
        return '<span class="label label-warning">二级告警</span>'
    } else if (value == 3) {
        return '<span class="label label-info">三级告警</span>'
    } else if (value == 4) {
        return '<span class="label label-success">四级告警</span>'
    }
}

//定时器
let flag = false;
let t2 = window.setInterval(getData, 30*1000);
let t3 = window.setInterval(getNum, 30*1000);

//刷新重启定时器
function reFresh() {
    $("#begin").val('')
    $("#end").val('')
    endTime = '';
    beginTime = '';
    alarmstatus= '',
    $('#exampleTable').bootstrapTable('refresh');
    getSum()
    getActive()
    getQueren()
    getNum()
    if (flag) {
        t2 = window.setInterval(getData, 30*1000);
        t3 = window.setInterval(getNum, 30*1000);
        flag = false;
    }

}

//同步
function tongbu() {
    beginTime = $("#begin").val()
    endTime = $("#end").val()
    $('#exampleTable').bootstrapTable('refresh');
    getSum()
    getActive()
    getQueren()
    getNum();
    if (endTime) {
        console.log("aaaaaaa")
        clearInterval(t2);
        clearInterval(t3);
        flag = true;
    }
    if (beginTime && !endTime) {
        endTime = '';
        beginTime = $("#begin").val();
    }
}

function getData() {
    // alert('666')
    reLoad()
}


function add0(m) {
    return m < 10 ? '0' + m : m
}

//时间格式化
function format(shijianchuo) {
    //shijianchuo是整数，否则要parseInt转换
    let time = new Date(shijianchuo);
    let y = time.getFullYear();
    let m = time.getMonth() + 1;
    let d = time.getDate();
    let h = time.getHours();
    let mm = time.getMinutes();
    let s = time.getSeconds();
    return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}

//各级预警查询
function getNum() {
    let arr = ['1', '2', '3', '4'];
    for (let i = 0; i < arr.length; i++) {
        $.ajax({
            cache: true,
            type: "get",
            url: "/domain/realtimealarm/count",
            data: {
                origseverity: arr[i],
                beginTime: beginTime,
                endTime: endTime
            },
            async: false,
            success: function (data) {
                $('#num' + arr[i]).html(data.total)
            }
        });
    }
}

//全部预警
function getSum() {
    $.ajax({
        cache: true,
        type: "get",
        url: "/domain/realtimealarm/count",
        data: {
            beginTime: beginTime,
            endTime: endTime
        },
        async: false,
        success: function (data) {
            $('#alarmSum').html(data.total)
        }
    });
}

function quanbu() {
    endTime = endTime;
    beginTime = beginTime;
    alarmstatus = '';
    $('#exampleTable').bootstrapTable('refresh');
}

//活动告警
function getActive() {
    $.ajax({
        cache: true,
        type: "get",
        url: "/domain/realtimealarm/count",
        data: {
            beginTime: beginTime,
            endTime: endTime,
            alarmstatus: 1,
        },
        async: false,
        success: function (data) {
            $('#activeAlarm').html(data.total)
        }
    });
}


function activeAlarm() {
    endTime = endTime;
    beginTime = beginTime;
    alarmstatus = 1;
    $('#exampleTable').bootstrapTable('refresh');
}

//确认告警
function getQueren() {
    $.ajax({
        cache: true,
        type: "get",
        url: "/domain/realtimealarm/count",
        data: {
            beginTime: beginTime,
            endTime: endTime,
            alarmstatus: 0,
        },
        async: false,
        success: function (data) {
            $('#querenAlarm').html(data.total)
        }
    });
}

function queAlarm() {
    endTime = endTime;
    beginTime = beginTime;
    alarmstatus = 0;
    $('#exampleTable').bootstrapTable('refresh');
}


/**
 * 导出
 */
function report(){
    let url = "/domain/realtimealarm/export?";
    url += "beginTime=" + beginTime;
    url += "&endTime=" + endTime;
    window.open(url, "_blank");
}
// $("#reportTable").on("click", function () {
//     console.log(status)
//
// });


